﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace VirtualOperator
{
    public partial class ConsolGUI : Form
    {
        private VirtualNetworkOperator administer;
        private TransportLayer.Tokener tokens = new TransportLayer.Tokener();
        public ConsolGUI(VirtualNetworkOperator administer)
        {
            this.administer = administer;
            InitializeComponent();
            outputTextBox.Text += ">> Welcome to VNO console!" + Environment.NewLine;
        }




        private void maskedTextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {

            if (e.KeyChar == (char)13) //13 ---> enter
            {

                if (inputTextBox.Text == "")
                    return;

                outputTextBox.Text += "<< " + inputTextBox.Text + Environment.NewLine;
                string result = administer.Communicator.ConsoleQuery(inputTextBox.Text);
                if(administer.Communicator.Connected)
                    outputTextBox.Text += ">> " + result + Environment.NewLine;
                else
                    outputTextBox.Text += ">> Sorry, but VNO is not connected to PicsNC." + Environment.NewLine;
                tokens.Put(inputTextBox.Text);
                inputTextBox.Text = "";
            }

            outputTextBox.SelectionLength = 0;

            outputTextBox.SelectionStart = outputTextBox.Text.Length;

            outputTextBox.ScrollToCaret();
        
        }

        private void ConsolGUI_Load(object sender, EventArgs e)
        {

        }

        private void inputTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Up)
            {
                    inputTextBox.SelectionStart = outputTextBox.Text.Length;
                    tokens.MoveUp();
                    inputTextBox.Text = tokens.CurrentToken;
                    inputTextBox.SelectionStart = outputTextBox.Text.Length;
                    
            }
            else if (e.KeyCode == Keys.Down)
            {
                    inputTextBox.SelectionStart = outputTextBox.Text.Length;
                    tokens.MoveDown();
                    inputTextBox.Text = tokens.CurrentToken;
                    inputTextBox.SelectionStart = outputTextBox.Text.Length;
            }
        }


    }
}
